﻿using Aliphant.Interfaces;
using Aliphant.IoC;
using Aliphant.Mvvm.XEvent.Interface;
using Aliphant.Mvvm.XEvent.Ioc;

using System.IO;
using System.Reflection;
using System.Windows;

namespace Aliphant.Mvvm.XEvent
{
    public abstract class XApplicationBase : Application
    {
        private IUIContainer _container = new UIContainer();
        private IDiContainer _diContainer = new DiContainer();

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            InitializeInternal();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            _container.Dispose();
            _diContainer.Dispose();
        }

        private void InitializeInternal()
        {
            Initialize();
        }

        private void Initialize()
        {
            _container = UIContainerLocator.Container;
            _diContainer = IoCServer.Container;

            // 动态注册模块
            var catalog = ConfigurationModule();
            catalog.Register(_container, _diContainer);

            // 手动注册模块
            RegisterTypes(_container);
            RegisterService(_diContainer);
            RegisterModule(_container, _diContainer);
        }

        /// <summary>
        /// 注册UI控件
        /// </summary>
        /// <param name="container"></param>
        public virtual void RegisterTypes(IUIContainer container) { }

        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="diContainer"></param>
        public virtual void RegisterService(IDiContainer diContainer) { }

        /// <summary>
        /// 注册模块
        /// </summary>
        /// <param name="container"></param>
        /// <param name="diContainer"></param>
        public virtual void RegisterModule(IUIContainer container, IDiContainer diContainer) { }

        public virtual IModuleCatalog ConfigurationModule() { return new XModuleCatalog(); }
    }
}
